package com.gin.no_gay_alliance.fragment.primsg;

import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;

import com.gin.common.utils.StrUtils;
import com.gin.jackson.utils.ObjectUtils;
import com.gin.nga.exception.NgaClientException;
import com.gin.nga.exception.NgaException;
import com.gin.nga.method.NukeApi;
import com.gin.nga.response.body.nuke.PmBlockListBody;
import com.gin.no_gay_alliance.adpter.pm.PriMsgBlockAdapter;
import com.gin.no_gay_alliance.app.MyApplication;
import com.gin.no_gay_alliance.callback.Callback;
import com.gin.no_gay_alliance.callback.RequestCallback;
import com.gin.no_gay_alliance.databinding.FragmentPriMsgBlockBinding;
import com.gin.no_gay_alliance.fragment.base.BaseBindingFragment;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

import okhttp3.Call;

/**
 * 私信黑名单
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/15 11:06
 **/
public class PriMsgBlockFragment extends BaseBindingFragment<FragmentPriMsgBlockBinding> implements Callback<PmBlockListBody> {

    private PriMsgBlockAdapter adapter;

    /**
     * onCreateView 的后续操作
     */
    @Override
    protected void onCreateView() {
        b.tvWarning.setVisibility(View.GONE);
        adapter = new PriMsgBlockAdapter(requireContext());
        b.recyclerView.setAdapter(adapter);
        b.swipeRefreshLayout.setOnRefreshListener(this::sendRequest);
        sendRequest();
    }

    /**
     * 请求成功回调
     *
     * @param data 数据
     */
    @Override
    public void onSuccess(PmBlockListBody data) {
        if (data.getData() != null && !ObjectUtils.isEmpty(data.getData().values())) {
            ui(() -> {
                b.swipeRefreshLayout.setRefreshing(false);
                b.recyclerView.setVisibility(View.VISIBLE);
                b.tvWarning.setVisibility(View.GONE);
                adapter.updateData(new ArrayList<>(data.getData().values()));
            });
        } else {
            showWarn("暂时没有数据");
        }
    }

    /**
     * 请求失败回调(一般为网络访问错误)
     *
     * @param call call
     * @param e    错误
     */
    @Override
    public void onFailure(@NonNull Call call, @NonNull IOException e) {
        Callback.super.onFailure(call, e);
        showWarn(e.getMessage());
    }

    /**
     * 处理Nga异常
     *
     * @param e 异常
     */
    @Override
    public void handleException(NgaException e) {
        Callback.super.handleException(e);

        if (e instanceof NgaClientException) {
            Map<Integer, String> reason = ((NgaClientException) e).getReason();
            if (reason != null) {
                String t0 = reason.get(0);
                String t1 = reason.get(1);
                if (!ObjectUtils.isEmpty(t0) && !StrUtils.isNumber(t0)) {
                    showWarn(t0);
                } else if (!ObjectUtils.isEmpty(t1) && !StrUtils.isNumber(t1)) {
                    showWarn(t1);
                }
            }
            Log.d(this.getClass().getSimpleName(), "handleException: " + e.getMessage());
        }
    }

    private void sendRequest() {
        ui(() -> {
            adapter.clear();
            b.swipeRefreshLayout.setRefreshing(true);
        });
        NukeApi.pmBlockList(MyApplication.getClient()).async(new RequestCallback<>(this));
    }

    private void showWarn(String text) {
        ui(() -> {
            b.swipeRefreshLayout.setRefreshing(false);
            b.recyclerView.setVisibility(View.GONE);
            b.tvWarning.setVisibility(View.VISIBLE);
            b.tvWarning.setText(text);
        });
    }
}
